package com.aim.Activity;

import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.aim.api.AIMCaseConstants;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class NobelMap extends MapActivity implements LocationListener{
	public MapView mapView=null;
	public LocationManager myLocationManager=null;
	private String bestProvider;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.maplayout);
	    mapView = (MapView) findViewById(R.id.mapview);
	    myLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
	    Criteria criteria = new Criteria();
//		criteria.setAccuracy(Criteria.ACCURACY_FINE);
//        criteria.setAltitudeRequired(false);
//        criteria.setBearingRequired(false);
//        criteria.setCostAllowed(true);
//        criteria.setPowerRequirement(Criteria.POWER_LOW);
        bestProvider = myLocationManager.getBestProvider(criteria, true);
	
		
		mapView.setBuiltInZoomControls(true);
	    SharedPreferences settings = getSharedPreferences(AIMCaseConstants.PREFS_NAME, 0);
	    mapView.setSatellite(settings.getBoolean("satellite",false));
	}
	@Override
	protected void onResume() {
		super.onResume();
		myLocationManager.requestLocationUpdates(bestProvider, 1000, 0, this);
		
	}

	/** Stop the updates when Activity is paused */
	@Override
	protected void onPause() {
		super.onPause();
		myLocationManager.removeUpdates(this);
	}

	public void onLocationChanged(Location location) {
		Log.e("changed",location.toString());
		double i = location.getLatitude();
		double j = location.getLongitude();
		Double lat = i*1000000;
		Double lon = j*1000000;
	CenterLocation(new GeoPoint(lat.intValue(),
			lon.intValue()));

	Log.e("cen",lat.intValue()+"      "+lon.intValue());
	mapView.getController().setZoom(20);
	}

	public void onProviderDisabled(String provider) {
		// let okProvider be bestProvider
		// re-register for updates
//		output.append("\n\nProvider Disabled: " + provider);
	}

	public void onProviderEnabled(String provider) {
		// is provider better than bestProvider?
		// is yes, bestProvider = provider
//		output.append("\n\nProvider Enabled: " + provider);
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		System.out.print("\n\nProvider Status Changed: " + provider + ", Status="
				 + ", Extras=" + extras);
	}
	@Override
	protected boolean isRouteDisplayed() {
	    return false;
	}
	private void CenterLocation(GeoPoint centerGeoPoint)
	{
		mapView.getController().animateTo(centerGeoPoint);
	}
	
	@Override  
    public boolean onCreateOptionsMenu(Menu menu) {   
        menu.add(0,Menu.FIRST,0,"satallite");   
        menu.add(0,Menu.FIRST+1,1,"zoom");   
        menu.add(0,Menu.FIRST+2,2,"gps");   
        return super.onCreateOptionsMenu(menu);   
           
    }   
	@Override  
    public boolean onOptionsItemSelected(MenuItem item) {   
//        TextView tv = (TextView)findViewById(R.id.tv01);   
		super.onOptionsItemSelected(item); 
        switch(item.getItemId()){   
            case Menu.FIRST:{
            	Log.e("","111 pressed");
            	mapView.setSatellite(!mapView.isSatellite());
    			SharedPreferences.Editor settings = getSharedPreferences(AIMCaseConstants.PREFS_NAME, 0).edit();
    			settings.putBoolean("satellite",mapView.isSatellite());
    			settings.commit();
            	break;   }
            case Menu.FIRST+1:{
//            	Log.e("","zoom pressed");
//            	mapView.displayZoomControls(true);
            	CenterLocation(new GeoPoint(11503422,-24452152));
 			
            	break;   }
            case Menu.FIRST+2:{
            	Location location = myLocationManager.getLastKnownLocation(bestProvider);
    			
    			if (location == null)
    				Log.e("location","null");
    			else{
    				Log.e("location",location.toString());
//    				int i = Integer.parseInt(location.getLatitude());
    				double i = location.getLatitude();
    				double j = location.getLongitude();
    				Double lat = i*1000000;
    				Double lon = j*1000000;
    			CenterLocation(new GeoPoint(lat.intValue(),
    					lon.intValue()));
    			
				Log.e("cen",lat.intValue()+"      "+lon.intValue());
				mapView.getController().setZoom(20);
//    			mapView.getController().setZoom(settings.getInt("zoom", 15));
//    			Log.e("stat","myLocationManager.getGpsStatus(null).toString()");
    			
    			String Text = "My current location is: " +
    			
    			"Latitud = " + location.getLatitude() +

    			"Longitud = " + location.getLongitude();
    			System.out.println(Text);
    			Toast.makeText( getApplicationContext(),

    					Text,

    					Toast.LENGTH_SHORT).show();}
    			Log.e("","gps pressed4");

            	break;   }
        }   
        return true;   
    }  
}
